package com.xuliugen.lsm.tree;

import java.util.ArrayList;
import java.util.List;

import com.xuliugen.lsm.tree.BTree.SearchResult;

public class BTreeNode<K extends Comparable<K>, V> {

    private int keyNum;
    private List<Entry<K, V>> entries;
    private List<BTreeNode<K, V>> children;
    private final int maxNodeSize;
    private boolean isLeaf;

    public BTreeNode(boolean isLeaf, int maxNodeSize) {
        this.maxNodeSize = maxNodeSize;
        entries = new ArrayList<Entry<K, V>>();
        children = new ArrayList<BTreeNode<K, V>>();
        this.isLeaf = false;
    }

    public boolean isLeaf() {
        return isLeaf;
    }

    public int size() {
        return entries.size();
    }

    public int getKeyNum() {
        return keyNum;
    }

    public void setLeaf(boolean isLeaf) {
        this.isLeaf = isLeaf;
    }

    public SearchResult searchKey(K key) {

        int left = 0;
        int right = entries.size() - 1;
        int mid, cmp;
        while (left <= right) {
            mid = (left + right) >> 1;
            cmp = entries.get(mid).getKey().compareTo(key);
            if (cmp == 0) {
                return new SearchResult(true, mid);
            } else if (cmp > 0) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return new SearchResult(false, left);
    }

    public boolean isFull() {
        return maxNodeSize == entries.size();
    }

    public List<BTreeNode<K, V>> getChildren() {
        return children;
    }

    public void setChildren(List<BTreeNode<K, V>> children) {
        this.children = children;
    }

    public void setKeyNum(int keyNum) {
        this.keyNum = keyNum;
    }

    public List<Entry<K, V>> getEntries() {
        return entries;
    }

    public void setEntries(List<Entry<K, V>> entries) {
        this.entries = entries;
    }

}
